export default function useSelection(props, emit) {
  const tableIns = ref(null);

  const selectedKeys = computed({
    get() {
      return props.selected;
    },
    set(v) {
      emit("update:selected", v);
    },
  });

  const handleSelectionChange = (val) => {
    // multipleSelection.value = val

    const keys = val.map((item) => props.rowKey(item));
    console.log("keys", keys);
    selectedKeys.value = keys;
  };

  onMounted(() => {
    props.data.forEach((row) => {
      const key = props.rowKey(row);
      tableIns.value.toggleRowSelection(row, props.selected.includes(key));
    });

    emit("update:instance", tableIns.value);
  });

  return {
    tableIns,
    selectedKeys,
    handleSelectionChange,
  };
}
